<?php
/**
 * Track request handler
 *
 * ...
 */
require_once dirname(__FILE__) .'/cse-init.php';

// fetch required objects from Cinnamon registry
$session = Cinnamon::registry('session');
$events = Cinnamon::registry('events');
$params = Cinnamon::registry('params');
$db = Cinnamon::registry('db');

// validate the site id
$count = $db->getValue("SELECT COUNT(site_id) FROM $db->sites WHERE site_id='". $db->escape($params->site_id) ."'"); 
if ( $count != 1 ) {
	exit;
}
unset($count);

// retrieve site information
$sql  = "SELECT * FROM $db->sites WHERE site_id=". intval($params->site_id);
$site = $db->getRecord($sql);

// check if the request domain matches the specified id
$request = parse_url($params->request_url);
$defined = parse_url($site->site_url);
$matches = false;

// try #1: exact match
$matches = ( strcmp($request['host'], $defined['host']) == 0 );

// try #2: match using 'ignore www' option
if ( !$matches && strcmp($site->ignore_www, 'y') == 0 ) {
	$matches = ( strcmp(
					preg_replace('/^www\./i', '', $request['host']),
					preg_replace('/^www\./i', '', $defined['host'])) == 0 );
}

// try #2: match using 'match all subdomains' option
if ( !$matches && strcmp($site->match_subdomains, 'y') == 0 ) {
	$matches = ( strcmp(
					preg_replace('/^[a-zA-Z0-9_]*\./is', '', $request['host']),
					preg_replace('/^www\./i', '', $defined['host'])) == 0 );
}

// not a valid match? I give up!!!
if ( !$matches ) {
	echo 'not matched!';
	exit;
}

// identify request type. default is page
if ( empty($params->request_type) ) 
	$params->request_type = 'page';

//
// PRE TRACK EVENT
//

// create event for pre-tracking
$event = new Event('preTrack');

// fetch and decode parameters
foreach ( $params as $name=>$value ) {
	$name = urldecode($name);
	$value = urldecode($value);
	$event->{$name} = $value;
}
$event->site_id = $params->site_id;

// trigger event
$events->dispatchEvent($event);
unset($event);

//
// TRACK EVENT
//

// create event for tracking
Cinnamon::loadClass('TrackEvent');
$event = new TrackEvent();

// fetch and decode parameters
foreach ( $params as $name=>$value ) {	
	$name = urldecode($name);
	$value = urldecode($value);
	$event->{$name} = $value;
}
$event->site_id = $params->site_id;
$event->site_url = $site->site_url;

// add default parameters to visits storage
$event->addParameter('visit_id', $session->getVisitIdentifier($site->site_id));
$event->addParameter('site_id', intval($site->site_id));
$event->addParameter('request_utc_datetime', $db->getDatetime());
$event->addParameter('request_datetime', $db->getDatetime(null, $site->utc_offset));
$event->addParameter('ip_address', $event->ip_address);
$event->addParameter('request_type', $event->request_type);
$event->addParameter('request_url', $event->request_url);
$event->addParameter('request_title', $event->request_title);

// trigger event
$events->dispatchEvent($event);

// insert parameters into visits table
$db->insert($db->requests, $event->getAllParameters());

//
// POST TRACK EVENT
//

// create event for post tracking
$event = new Event('postTrack');

// fetch and decode parameters
foreach ( $params as $name=>$value ) {
	$name = urldecode($name);
	$value = urldecode($value);
	$event->{$name} = $value;
}
$event->site_id = $params->site_id;
$event->visit_id = $db->insertId();

// trigger event
$events->dispatchEvent($event);
unset($event);

// redirect for feeds and downloads
if ( $params->request_type == 'download' ) {
	header('Location: '. $params->request_url);
}

exit;	// that's all folks!
?>